package com.financing.wap.web.controller.api;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.MessageDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IMessageService;
import com.financing.wap.util.Constants;
import com.financing.wap.vo.MessageVO;
import com.financing.wap.web.controller.BaseController;

/**
 * 消息API
 * @author jtl
 *
 */
@Controller("apiMessageController")
@RequestMapping("/api/message")
public class MessageController extends BaseController {
	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IMessageService messageService;


	@RequestMapping("list")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, HttpServletResponse response,MessageVO msg){

		ResultMap result = new ResultMap();
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isBlank(pageNo)){
			pageNo="1";
		}
		
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		msg.setLoginUser(faId);
		try {
			Integer count = messageService.getMessageCount(msg);
			Pager pager = new Pager();
			pager.setTotalCount(count);
			pager.setPageSize(msg.getPageSize());
			pager.setPageNo(Integer.parseInt(pageNo));
			msg.setStart(pager.getStartIndex());
			
			List<MessageDTO> MessageList = messageService.getMessageList(msg);
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("list", MessageList);
			result.setAttribute("pager", pager);
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setStatus(STATUS.ERROR);
		}
		return result;
	}

	
	@RequestMapping("msgStatus")
	@ResponseBody
	public ResultMap messageStatus(HttpServletRequest request, HttpServletResponse response,MessageVO msg){

		ResultMap result = new ResultMap();
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isBlank(pageNo)){
			pageNo="1";
		}
		Integer count =0;
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setStatus(STATUS.SUCCESS);
		if(null==loginUser){
			
			result.setAttribute("loginStatus", "0");//未登陆状态
			
		}else{
			//当前登录人ID
			String faId = loginUser.getId();
			msg.setLoginUser(faId);
			try {
				result.setAttribute("loginStatus", "1");//未登陆状态
				msg.setMsgStatus(Constants.MESSAGE_STATUS_NOSHOW);
//				count = messageService.getMessageCount(msg);
			}catch(Exception e) {
				logger.warn("==>warn messang:"+e.getMessage());
				result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
				result.setStatus(STATUS.ERROR);
			}
		}
		result.setAttribute("msgCount", count);
		return result;
	}
	
	
	@RequestMapping(value="/view/{id}",method=RequestMethod.GET)
	@ResponseBody
	public ResultMap view(@PathVariable int id, HttpServletRequest request, HttpServletResponse response){
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		try {
			Map<String,Object> paramsMap = new HashMap<String,Object>();
			paramsMap.put("id", id);
			paramsMap.put("loginUserId", faId);
			MessageDTO messageDto = messageService.getMessageView(paramsMap);
			if(messageDto==null){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_OBJECT_NOFOUNT);
				return result;
			}
			logger.info("=====view success====");
			messageDto.setLoginUser(faId);
			messageDto.setMsgStatus(Constants.MESSAGE_STATUS_SHOW);
			messageService.updateMessageStatus(messageDto);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setAttribute("messageDto", messageDto);
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setStatus(STATUS.ERROR);
		}
		return result;
	}
	
	@RequestMapping("/mark/{id}")
	@ResponseBody
	public ResultMap markAndRead(@PathVariable int id,HttpServletRequest request, HttpServletResponse response,MessageDTO msg){

		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		ResultMap result = new ResultMap();
		try {
			MessageDTO messageDto = new MessageDTO();
			messageDto.setId(id);
			messageDto.setLoginUser(faId);
			messageDto.setMsgStatus(Constants.MESSAGE_STATUS_SHOW);
			messageService.updateMessageStatus(messageDto);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e) {
			logger.warn("==>warn messang:"+e.getMessage());
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setStatus(STATUS.ERROR);
		}
		return result;
	}
}
